#!/usr/bin/env python
# coding=utf-8
#--------------------------------------------------------------------------
# 文件名:       WpXMLRPC.py 
# 说明：        目的、作用说明
#
# 版本：        0.01
# 作者：        xiao
#
# 创建时间：    2011-02-14 
# 许可证：      Apache License V2.0
#--------------------------------------------------------------------------

import xmlrpclib
from pprint import pprint

class WpXMLRPC:
	
	def __init__(self, wp_url, username, password, blog_type="wp", blog_id=0, verbose=False):
		self.wp_url = wp_url
		self.user = username
		self.password = password

		xmlrpc_url = "%s/xmlrpc.php" % self.wp_url.strip("/")
		if blog_type == "zblog":
			xmlrpc_url = "%s/xml-rpc/index.asp" % self.wp_url.strip("/")

		self.server = xmlrpclib.ServerProxy(xmlrpc_url)
		self.blog_id = blog_id,
		self.verbose = verbose
		
	def _print(self, msg):
		if self.verbose:
			print(msg)

	def xmlrpcList(self):
		pprint(self.server.system.listMethods())

	def getUsersBlogs(self):
		# 检查这个帐号和BLOG
		# http://codex.wordpress.org/XML-RPC_wp#wp.getUsersBlogs
		results = self.server.wp.getUsersBlogs(
			self.user,
			self.password
		)
		return results

	def getTags(self):
		# Get list of all tags.
		# http://codex.wordpress.org/XML-RPC_wp#wp.getTags
		results = self.server.wp.getTags(
			self.blog_id,
			self.user,
			self.password
		)
		return results

	def getCommentCount(self, post_id):
		# Retrieve comment count for a specific post.
		# http://codex.wordpress.org/XML-RPC_wp#wp.getCommentCount
		results = self.server.wp.getCommentCount(
			self.blog_id,
			self.user,
			self.password,
			post_id
		)
		return results

	def getPostStatusList(self):
		# Retrieve post statuses.
		# http://codex.wordpress.org/XML-RPC_wp#wp.getPostStatusList
		results = self.server.wp.getPostStatusList(
			self.blog_id,
			self.user,
			self.password
		)
		return results

	def getPageStatusList(self):
		# Retrieve all of the WordPress supported page statuses.
		# http://codex.wordpress.org/XML-RPC_wp#wp.getPageStatusList
		results = self.server.wp.getPageStatusList(
			self.blog_id,
			self.user,
			self.password
		)
		return results

	def getPageTemplates(self):
		# Retrieve page templates.
		# http://codex.wordpress.org/XML-RPC_wp#wp.getPageTemplates
		results = self.server.wp.getPageTemplates(
			self.blog_id,
			self.user,
			self.password
		)
		return results

	def getOptions(self, option_array):
		# Retrieve blog options. If passing in an array, search for options listed within it.
		# http://codex.wordpress.org/XML-RPC_wp#wp.getOptions
		results = self.server.wp.getOptions(
			self.blog_id,
			self.user,
			self.password,
			option_array
		)
		return results

	def setOptions(self, struct_array):
		# Update blog options. Returns array of structs showing updated values.
		# http://codex.wordpress.org/XML-RPC_wp#wp.setOptions
		results = self.server.wp.setOptions(
			self.blog_id,
			self.user,
			self.password,
			struct_array
		)
		return results

	def deleteComment(self, comment_id):
		# 删除评论
		# http://codex.wordpress.org/XML-RPC_wp#wp.deleteComment
		results = self.server.wp.deleteComment(
			self.blog_id,
			self.user,
			self.password,
			comment_id
		)
		return results

	def editComment(self, comment_id, struct_comment):
		# 编辑评论
		# http://codex.wordpress.org/XML-RPC_wp#wp.editComment
		results = self.server.wp.editComment(
			self.blog_id,
			self.user,
			self.password,
			comment_id,
			struct_comment
		)
		return results

	def newComment(self, post_id, struct_comment):
		# 添加评论
		# http://codex.wordpress.org/XML-RPC_wp#wp.newComment
		results = self.server.wp.newComment(
			self.blog_id,
			self.user,
			self.password,
			post_id,
			struct_comment
		)
		return results

	def getCommentStatusList(self):
		# Retrieve all of the comment status.
		# http://codex.wordpress.org/XML-RPC_wp#wp.getCommentStatusList
		results = self.server.wp.getCommentStatusList(
			self.blog_id,
			self.user,
			self.password
		)
		return results

	def getPage(self, page_id):
		# Get the page identified by the page id.
		# http://codex.wordpress.org/XML-RPC_wp#wp.getPage
		results = self.server.wp.getPage(
			self.blog_id,
			page_id,
			self.user,
			self.password
		)
		return results

	def getPages(self):
		# Get an array of all the pages on a blog.
		# http://codex.wordpress.org/XML-RPC_wp#wp.getPages
		results = self.server.wp.getPages(
			self.blog_id,
			self.user,
			self.password
		)
		return results

	def getPageList(self):
		# Get an array of all the pages on a blog. Just the minimum details, lighter than wp.getPages.
		# http://codex.wordpress.org/XML-RPC_wp#wp.getPageList
		results = self.server.wp.getPageList(
			self.blog_id,
			self.user,
			self.password
		)
		return results

	def newPage(self, struct_content, publish=True):
		# Create a new page. Similar to metaWeblog.newPost.
		# http://codex.wordpress.org/XML-RPC_wp#wp.newPage
		results = self.server.wp.newPage(
			self.blog_id,
			self.user,
			self.password,
			struct_content,
			publish
		)
		return results

	def deletePage(self, page_id):
		# Removes a page from the blog.
		# http://codex.wordpress.org/XML-RPC_wp#wp.deletePage
		results = self.server.wp.deletePage(
			self.blog_id,
			self.user,
			self.password,
			page_id
		)
		return results

	def newPost(self, struct_content, publish=True):
		results = self.server.metaWeblog.newPost(
			self.blog_id,
			self.user,
			self.password,
			struct_content,
			publish
		)
		return results

	def newCategory(self, struct_category):
		results = self.server.wp.newCategory(
			self.blog_id,
			self.user,
			self.password,
			struct_category
		)
		return results
		
	def getCategories(self):
		results = self.server.wp.getCategories(
			self.blog_id,
			self.user,
			self.password
		)
		return results
		


if __name__ == "__main__":
	wp = WpXMLRPC("http://www.morncity.com/blog/", "admin", "AHHFqUgEM8vN", 0)
	wp.newCategory({"name":"ts", "description":"ts_sdf"})
	pprint(wp.newPost({"title":"tTdg,Tag  le", "description":"testPost", "mt_keywords":"testTag,test2,woe,Eliminate toxic substance"}))

	#cys = wp.getCategories()
	#cynames = [cy["categoryName"] for cy in cys]
	#if "Men's Health" in cynames:
		#print("Y!")
	#pprint(wp.getCategories())
